//
//  UIApplication+TS.swift
//  TSKit
//
//  Created by chenyusen on 2018/3/16.
//

import Foundation


// MARK: - 属性扩展
public extension UIApplication {
    
    
    /// 当前App沙盒的documentsURL
    var documentsURL: URL {
        return FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory,
                                        in: FileManager.SearchPathDomainMask.userDomainMask).last!
    }
    /// 当前App沙盒的documents路径
    var documentsPath: String {
        return NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,
                                                   FileManager.SearchPathDomainMask.userDomainMask,
                                                   true).first!
    }
    
    /// 当前App沙盒的cachesURL
    var cachesURL: URL {
        return FileManager.default.urls(for: FileManager.SearchPathDirectory.cachesDirectory,
                                        in: FileManager.SearchPathDomainMask.userDomainMask).last!
    }
    
    
    /// 当前App沙盒的caches路径
    var cachesPath: String {
        return NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory,
                                                   FileManager.SearchPathDomainMask.userDomainMask,
                                                   true).first!
    }
    
    /// 当前App沙盒的LibraryURL
    var LibraryURL: URL {
        return FileManager.default.urls(for: FileManager.SearchPathDirectory.libraryDirectory,
                                        in: FileManager.SearchPathDomainMask.userDomainMask).last!
    }
    
    /// 当前App沙盒的Library路径
    var LibraryPath: String {
        return NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory,
                                                   FileManager.SearchPathDomainMask.userDomainMask,
                                                   true).first!
    }
    
    
    /// 当前App沙盒的临时路径
    var tempPath: String {
        return NSTemporaryDirectory()
    }
    
    
    /// 当前App的BundleName
    var appBundleName: String {
        return Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
    }
    
    /// 当前App的BundleID
    var appBundleID: String {
        return Bundle.main.object(forInfoDictionaryKey: "CFBundleIdentifier") as! String
    }
    
    /// 当前App的版本号
    var appVersion: String {
        return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
    }
    
    /// 当前App的构建版本号
    var appBuildVersion: String {
        return Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String
    }
}


// MARK: - 方法扩展
public extension UIApplication {
    
    /// 获取当前处于最顶层的控制器
    ///
    /// - Parameter controller: 从某个跟控制器开始遍历, 默认为当前的keyWindow的根控制器
    /// - Returns: 遍历到的当前最顶层控制器
    public func topViewController(_ rootController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navigationController = rootController as? UINavigationController {
            return topViewController(navigationController.visibleViewController)
        }
        if let tabController = rootController as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(selected)
            }
        }
        if let presented = rootController?.presentedViewController {
            return topViewController(presented)
        }
        return rootController
    }
}
